/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.center.web.action.performance;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;

import com.sinodata.bsm.center.bean.PropertyBean;
import com.sinodata.bsm.center.bean.ResBean;
import com.sinodata.bsm.center.bean.ResTypeBean;
import com.sinodata.bsm.center.cache.CollectFieldCache;
import com.sinodata.bsm.center.service.performance.PropValueService;
import com.sinodata.bsm.center.service.resource.CollectTaskService;
import com.sinodata.bsm.center.service.resource.PropertyService;
import com.sinodata.bsm.center.util.spring.SpringContextHolder;
import com.sinodata.bsm.center.util.web.Struts2Utils;
import com.sinodata.bsm.center.web.action.BaseAction;
import com.sinodata.bsm.common.vo.CollectField;
import com.sinodata.bsm.common.vo.CollectTask;
import com.sinodata.bsm.common.vo.Property;
import com.sinodata.bsm.common.vo.PropertyValue;
import com.sinodata.bsm.common.vo.Res;

/**
 * <p>
 * Description: 指标趋势分析类
 * </p>
 *
 * @author wangyazhou
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * Jul 13, 2012 5:08:14 PM          wangyazhou        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class PropTrendAction extends BaseAction {

    private Long id;

    @Autowired
    private PropValueService propService;

    private static String[] colors = { "0080C0", "00ff00", "030350", "FF8040", "FFFF00", "FF0080", "29d4c7", "c7b28a" };
    @Autowired
    private PropertyService propertyService;

    @Autowired
    CollectTaskService collectTaskService;

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private final SimpleDateFormat oriSdf = new SimpleDateFormat("MM-dd HH:mm");
    private final SimpleDateFormat hourSdf = new SimpleDateFormat("MM-dd HH");
    private final SimpleDateFormat daySdf = new SimpleDateFormat("MM-dd");

    private final static String ORINIANL = "0";
    private final static String HOURAGGR = "1";

    //    private final static String DAYAGGR = "2";

    /**
     * 生成指标分组数据
     * @return
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public String getPropGroup() {
        String resId = request.getParameter("resId");
        try {
            ResBean rbean = ResBean.get(Long.parseLong(resId));
            if (rbean == null) {
                return null;
            }
            ResTypeBean bean = ResTypeBean.get(rbean.res().getResTypeId());
            if (bean == null) {
                return null;
            }

            List<CollectTask> resTaskList = collectTaskService.findByResId(rbean.res().getId());//采集器实例
            List<CollectTask> taskList = new ArrayList<CollectTask>(); //显示的结果
            List<CollectTask> typelist = collectTaskService.findByResTypeId(bean.resType().getId());
            if (resTaskList != null) {
                taskList.addAll(resTaskList);
                for (int i = 0; typelist != null && i < typelist.size(); i++) {
                    CollectTask task = typelist.get(i);
                    boolean hasConf = false;
                    for (CollectTask task2 : resTaskList) {
                        if (task2.getResTypeTaskId() != null && task.getId().longValue() == task2.getResTypeTaskId()) {
                            hasConf = true;
                        }
                    }
                    if (!hasConf) {
                        taskList.add(task);
                    }
                }
            } else if (typelist != null) {
                taskList.addAll(typelist);
            }

            //批量采集时采集任务在父资源上
            taskList.addAll(collectTaskService.findFatherTaskByResId(Long.parseLong(resId)));

            CollectFieldCache collectFieldCache = SpringContextHolder.getBean(CollectFieldCache.class);
            Set<Long> propSet = new HashSet<Long>();
            for (CollectTask task : taskList) {
                List<CollectField> fields = collectFieldCache.get(task.getId());
                if (fields != null) {
                    for (CollectField field : fields) {
                        propSet.add(field.getProp());
                    }
                }
            }

            List<Property> properties = bean.properties();

            List<Property> definedProperty = propertyService.findPropertyByResId(Long.parseLong(resId));
            if (definedProperty != null) {
                properties.addAll(definedProperty);
            }

            List<Property> validProps = new ArrayList<Property>();
            for (Property p : properties) {
                if (propSet.contains(p.getId())) {
                    validProps.add(p);
                }
            }

            JSONArray dataset = new JSONArray();
            //把指标分组
            Map<String, List<Property>> propMap = groupPropByUnit(validProps);
            Iterator it = propMap.entrySet().iterator();
            while (it.hasNext()) {
                JSONObject json = new JSONObject();

                java.util.Map.Entry entry = (java.util.Map.Entry) it.next();
                String key = (String) entry.getKey();
                List<Property> props = (List<Property>) entry.getValue();
                json.element("unit", key.length() == 0 ? "无" : key);

                JSONArray data = new JSONArray();
                for (int i = 0; i < props.size(); i++) {
                    JSONObject obj = new JSONObject();
                    Property prop = props.get(i);
                    obj.element("id", prop.getId());
                    obj.element("name", prop.getName());
                    data.add(obj);
                }
                json.element("data", data);
                dataset.add(json);
            }

            //todo test
            //            System.out.println(dataset.toString());
            Struts2Utils.renderText(dataset.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 生成一个资源的所有指标趋势图
     * @return
     */
    public String createTrend() {
        String resId = request.getParameter("resId");
        String propIds = request.getParameter("propIds");
        String dimen = request.getParameter("dimen");
        String trendtype = request.getParameter("trendtype");
        String timeSection = request.getParameter("timeSection");
        String beginDate = request.getParameter("beginDate");
        String endDate = request.getParameter("endDate");
        JSONObject json = new JSONObject();
        try {
            List<Property> properties = new ArrayList<Property>();
            String[] tmp = propIds.split("[,]");
            int[] propids = new int[tmp.length];
            for (int i = 0; i < tmp.length; i++) {
                Property p = PropertyBean.get(Long.parseLong(tmp[i])).property();
                propids[i] = p.getStore();
                properties.add(p);
            }

            List<PropertyValue> list = null;
            if (ORINIANL.equals(dimen)) {
                list = propService.getPropValues(Long.parseLong(resId), propids, DateGeneric.genericStartDate(timeSection, beginDate), DateGeneric.getnericEndDate2(dimen, timeSection, endDate), dimen);
            } else {
                list = propService.getTrendPropValus(Long.parseLong(resId), propids, DateGeneric.genericStartDate(timeSection, beginDate), DateGeneric.getnericEndDate2(dimen, timeSection, endDate), trendtype, dimen);
            }

            String title = buildTitle(resId, timeSection, beginDate, endDate, trendtype, dimen);

            json = buildJsonFor2Line(resId, properties, list, title, dimen);

            Struts2Utils.renderText(json.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 
     * @param resId  资源ID
     * @param timeSection  时间范围
     * @param beginDate  开始日期
     * @param endDate   结束日期
     * @param trendtype  趋势类型
     * @param dimen    数据维度
     * @return
     */
    private String buildTitle(String resId, String timeSection, String beginDate, String endDate, String trendtype, String dimen) {
        StringBuilder sb = new StringBuilder("[");
        try {
            if (ORINIANL.equals(dimen)) {
                sb.append("原始数据");
            } else if (HOURAGGR.equals(dimen)) {
                sb.append("时汇聚");
                sb.append("0".equals(trendtype) ? "-均值" : "1".equals(trendtype) ? "-峰值" : "-谷值");
            } else {
                sb.append("日汇聚");
                sb.append("0".equals(trendtype) ? "-均值" : "1".equals(trendtype) ? "-峰值" : "-谷值");
            }
            sb.append("趋势图]  [资源：");
            sb.append(ResBean.get(Long.parseLong(resId)).res().getName() + "]  [时间：");
            sb.append(DateGeneric.genericStartDate(timeSection, beginDate));
            sb.append(" -- ");
            sb.append(DateGeneric.getnericEndDate2(dimen, timeSection, endDate));
            sb.append("]");
        } catch (Exception e) {
            logger.error(e);
        }
        return sb.toString();
    }

    /**
     * 为二维线图构建JSON数据
     * @param list
     * @param propArry
     * @return
     */
    private JSONObject buildJsonFor2Line(String resId, List<Property> properties, List<PropertyValue> list, String title, String dimen) {
        JSONObject json = new JSONObject();
        JSONObject chart = new JSONObject();
        chart.element("showvalues", "0");
        chart.element("caption", title);
        //export 
        chart.element("exportEnabled", "1");
        chart.element("exportAtClient", "1");
        chart.element("exportHandler", "fcExporter1");
        chart.element("exportDialogMessage", "正在导出 请稍等...");
        chart.element("exportFormats", "PDF=导出为PDF|PNG=导出为PNG图片|JPG=导出为JPG图片");

        //        chart.element("exportTargetWindow", "_self");
        //        chart.element("exportAction", "download");
        //        chart.element("exportFileName", "demo");
        //        chart.element("showExportDialog", "1");

        //        chart.element("linethickness", "2");
        chart.element("showLabels ", "1");
        chart.element("numdivlines", "9");
        chart.element("numvdivlines", "19");
        chart.element("slantLabels", "1");
        chart.element("anchoralpha", "100");
        //        chart.element("anchorradius", "2");
        chart.element("rotateLabels", "1");
        //        chart.element("useEllipsesWhenOverflow ", "1");
        //        chart.element("labelDisplay ", "STAGGER");
        chart.element("adjustDiv", "1");

        //        chart.element("formatnumberscale", "1");
        chart.element("showalternatevgridcolor", "0");
        chart.element("anchorbgalpha", "50");

        chart.element("showShadow", "0");
        chart.element("divLineIsDashed", "1");
        chart.element("showAlternateHGridColor", "0");
        chart.element("showAlternateVGridColor", "0");
        chart.element("alternateVGridColor", "ffffff");
        //        chart.element("showLimits", "0");
        chart.element("adjustDiv", "1");
        //        chart.element("showZeroPlaneValue", "0");
        chart.element("chartLeftMargin", "50");
        chart.element("valueOnRight", "1");

        chart.element("bgcolor", "FFFFFF");
        chart.element("bordercolor", "FFFFFF");
        chart.element("canvasBorderThickness", "1");
        //        chart.element("canvasBorderColor", "ff0000");
        chart.element("showlegend", "1");
        chart.element("animation", "1");
        chart.element("divlinedecimalprecision", "1");
        chart.element("limitsdecimalprecision", "1");
        chart.element("numberSuffix", properties.get(0).getDataUnit() == null ? "" : properties.get(0).getDataUnit());

        //chart.element("baseFontSize", "12");

        //chart.element("numbersuffix", PropertyBean.get(Long.parseLong(propArry[0])).property().getDataUnit());

        //计算步长
        int step = list.size() / 20 + 1;
        chart.element("labelStep", step);

        JSONArray jj = new JSONArray();
        JSONObject o = new JSONObject();
        JSONArray labelJson = new JSONArray();
        if (ORINIANL.equals(dimen)) {
            for (PropertyValue p : list) {
                JSONObject obj = new JSONObject();
                obj.element("label", oriSdf.format(p.getTime()));
                labelJson.add(obj);
            }
        } else if (HOURAGGR.equals(dimen)) {
            for (PropertyValue p : list) {
                JSONObject obj = new JSONObject();
                obj.element("label", hourSdf.format(p.getTime()));
                labelJson.add(obj);
            }
        } else {
            for (PropertyValue p : list) {
                JSONObject obj = new JSONObject();
                obj.element("label", daySdf.format(p.getTime()));
                labelJson.add(obj);
            }
        }

        o.element("category", labelJson);
        jj.add(o);

        JSONArray dataset = new JSONArray();

        for (int j = 0; j < properties.size(); j++) {
            JSONObject obj = new JSONObject();
            Property pt = properties.get(j);
            //            String unit = (pt.getDataUnit() == null || pt.getDataUnit().length() == 0) ? "" : "(" + pt.getDataUnit() + ")";
            obj.element("seriesname", pt.getName()); //曲线名称
            obj.element("color", getRandomColor(j));
            JSONArray ja = new JSONArray();
            for (PropertyValue p : list) {
                JSONObject n = new JSONObject();
                String val = p.getValues()[j];
                n.element("value", (val == null || "".equals(val)) ? "0" : p.getValues()[j]);
                ja.add(n);
            }
            obj.element("data", ja);
            dataset.add(obj);
        }

        //当指标只有一个时  增加一个水平线
        if (properties.size() == 1) {
            JSONArray trendlines = new JSONArray();
            JSONObject linesObj = new JSONObject();
            JSONArray lines = new JSONArray();
            JSONObject line = new JSONObject();
            Double startVal = 0D;
            for (PropertyValue p : list) {
                startVal += Double.parseDouble(p.getValues()[0]);
            }
            line.element("startvalue", (list == null || list.size() < 1) ? "0" : startVal / list.size());
            line.element("color", "ff0000");
            line.element("displayvalue", "平均值");
            line.element("thickness", "3");
            lines.add(line);
            linesObj.element("line", lines);
            trendlines.add(linesObj);
            json.element("trendlines", trendlines);
        }

        json.element("categories", jj);
        json.element("chart", chart);
        json.element("dataset", dataset);

        JSONObject result = new JSONObject();
        result.element("size", list.size());
        result.element("value", json);
        return result;
    }

    /**
     * 为趋势图构建json对象
     * @param resId  资源ID
     * @param properties 资源指标列表
     * @param list   指标值结果
     * @return
     */
    //    @SuppressWarnings({ "rawtypes", "unchecked" })
    //    private JSONObject buildJsonForTrend(String resId, List<Property> properties, List<PropertyValue> list) {
    //        JSONObject result = new JSONObject();
    //        if (list == null) {
    //            result.element("size", "0");
    //            return result;
    //        }
    //        JSONObject json = new JSONObject();
    //        JSONArray axis = new JSONArray();
    //        JSONObject chart = new JSONObject();
    //
    //        //---------
    //
    //        //        chart.element("showvalues", "0");
    //        //        chart.element("linethickness", "2");
    //        //        chart.element("showLabels ", "1");
    //        //        chart.element("numvdivlines", "4");
    //        //        chart.element("slantLabels", "1");
    //        //        //        chart.element("anchorradius", "2");
    //        //        chart.element("rotateLabels", "1");
    //        //        //        chart.element("useEllipsesWhenOverflow ", "1");
    //        //        //        chart.element("labelDisplay ", "STAGGER");
    //        //        //        chart.element("adjustDiv ", "1");
    //        //
    //        //        chart.element("canvasbgcolor", "FFFFFF");
    //        //        chart.element("canvasbgangle", "270");
    //        //        chart.element("alternateHGridColor", "FFFFFF");
    //        //        chart.element("canvasBorderThickness", "0");
    //        //        chart.element("canvasBorderColor", "f1f1f1");
    //        //        chart.element("baseFontColor", "1D8BD1");
    //        //        chart.element("plotGradientColor", "DCE6F9");
    //        //        chart.element("divlinecolor", "FFFFFF");
    //        //        chart.element("divlinealpha", "60");
    //        //        chart.element("chartBottomMargin", "-5");
    //        //
    //        //        chart.element("labelStep", "1");
    //        //
    //        //        chart.element("bgcolor", "FFFFFF");
    //        //        chart.element("bordercolor", "FFFFFF");
    //        //        chart.element("showlegend", "0");
    //
    //        //--------------------------------
    //
    //        chart.element("showvalues", "0");
    //        chart.element("divlinealpha", "100");
    //        chart.element("bgColor", "FFFFFF");
    //        chart.element("borderColor", "FFFFFF");
    //        chart.element("numvdivlines", "4");
    //        chart.element("vdivlinealpha", "0");
    //        chart.element("canvasPadding", "0");
    //        //        chart.element("showalternatevgridcolor", "1");
    //        //        chart.element("anchorradius", "3");
    //        chart.element("alternatevgridalpha", "7");
    //        //        chart.element("showAlternateVGridColor", "1");
    //        //        chart.element("slantLabels", "1");
    //        //
    //        //        chart.element("showZeroPlaneValue", "0");
    //        //        chart.element("showLegend", "1");
    //        //        chart.element("legendAllowDrag", "1");
    //        //        chart.element("reverseLegend", "1");
    //        //        chart.element("interactiveLegend", "1");
    //        //        //        chart.element("legendNumColumns", "10");
    //        //        chart.element("minimiseWrappingInLegend", "1");
    //
    //        chart.element("xAxisName", "时间轴");
    //        //        chart.element("divLineAlpha", "100");
    //        //        chart.element("numVDivLines", "4");
    //        //        chart.element("vDivLineAlpha", "0");
    //        //        chart.element("showAlternateVGridColor", "1");
    //        //        chart.element("alternateVGridAlpha", "5");
    //
    //        //X轴
    //        JSONArray categories = new JSONArray();
    //        JSONObject cateObj = new JSONObject();
    //        JSONArray category = new JSONArray();
    //        for (int i = 0; i < list.size(); i++) {
    //            JSONObject o = new JSONObject();
    //            String time = list.get(i).getTime().toString();
    //            o.element("label", time.substring(11, time.length() - 8));
    //            category.add(o);
    //        }
    //        cateObj.element("category", category);
    //        categories.add(cateObj);
    //
    //        int count = 0;
    //        Map<String, List<Property>> propMap = groupPropByUnit(properties);
    //        Iterator it = propMap.entrySet().iterator();
    //        while (it.hasNext()) {
    //            java.util.Map.Entry entry = (java.util.Map.Entry) it.next();
    //            String key = (String) entry.getKey();
    //            List<Property> props = (List<Property>) entry.getValue();
    //            JSONObject axisObj2 = new JSONObject();
    //            axisObj2.element("title", key.length() == 0 ? "个" : key); //纵坐标 
    //
    //            if (count == 0) {
    //                axisObj2.element("titlepos", "left");
    //            } else {
    //                axisObj2.element("titlepos", "right");
    //                axisObj2.element("axisonleft", "0");
    //            }
    //            count++;
    //            //axisObj2.element("axisonleft", "0");
    //
    //            axisObj2.element("tickwidth", "10");
    //            axisObj2.element("divlineisdashed", "1");
    //            JSONArray dataset2 = new JSONArray();
    //            for (int i = 0; i < props.size(); i++) {
    //                Property prop = props.get(i);
    //                JSONObject data2 = new JSONObject();
    //
    //                data2.element("seriesName", prop.getName()); //指标名称
    //                data2.element("lineThickness", 3);
    //                data2.element("color", "CC0000");
    //                JSONArray j2y = new JSONArray();
    //                for (int j = 0; j < list.size(); j++) {
    //                    JSONObject jk = new JSONObject();
    //                    String value = list.get(j).getValues()[i];
    //                    jk.element("value", value == null ? "" : value);
    //                    j2y.add(jk);
    //                }
    //                data2.element("set", j2y);
    //                dataset2.add(data2);
    //            }
    //            axisObj2.element("dataset", dataset2);
    //            axis.add(axisObj2);
    //        }
    //
    //        json.element("chart", chart);
    //        json.element("categories", categories);
    //        json.element("axis", axis);
    //
    //        result.element("size", list.size());
    //        result.element("value", json);
    //
    //        return result;
    //    }
    /**
     * 按指标的单位 作分组
     * @param properties
     * @return
     */
    private Map<String, List<Property>> groupPropByUnit(List<Property> properties) {
        Map<String, List<Property>> map = new HashMap<String, List<Property>>();
        for (Property p : properties) {
            if (p.getDataTypeId() == 2) {
                String key = p.getDataUnit() == null ? "" : p.getDataUnit();
                List<Property> pList = map.get(key);
                if (pList == null) {
                    pList = new ArrayList<Property>();
                    map.put(key, pList);
                }
                pList.add(p);
            }
        }
        return map;
    }

    private String getRandomColor(int i) {
        return colors[i % 6];
    }

    /**
     * 进程/端口性能图
     * @return
     */
    public String createProcess() {
        String resId = request.getParameter("resId");
        String propIds = request.getParameter("propIds");
        String dimen = request.getParameter("dimen");
        String trendtype = request.getParameter("trendtype");
        String timeSection = request.getParameter("timeSection");
        String endDate = "";
        String beginDate = "";
        if (timeSection.equals("4") || timeSection.equals("1")) {
            timeSection = "4";
            String format = "yyyyMMdd HH";
            SimpleDateFormat simple = new SimpleDateFormat(format);
            endDate = simple.format(new Date());
            beginDate = simple.format(new Date(new Date().getTime() - 3600000 * 24));
        }
        JSONObject json = new JSONObject();
        try {
            List<Property> properties = new ArrayList<Property>();
            String[] tmp = propIds.split("[,]");
            int[] propids = new int[tmp.length];
            for (int i = 0; i < tmp.length; i++) {
                Property p = PropertyBean.get(Long.parseLong(tmp[i])).property();
                propids[i] = p.getStore();
                properties.add(p);
            }
            List<PropertyValue> list = propService.getTrendPropValus(Long.parseLong(resId), propids, DateGeneric.genericStartDate(timeSection, beginDate), DateGeneric.getnericEndDate2(dimen, timeSection, endDate), trendtype, dimen);
            json = buildJsonForProcessLine(resId, properties, list, "");
            Struts2Utils.renderText(json.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private JSONObject preparChart(String resId) {
        Res res = ResBean.get(Long.parseLong(resId)).res();
        JSONObject chart = new JSONObject();
        chart.element("showvalues", "0");
        chart.element("numdivlines", "9");
        chart.element("numvdivlines", "19");
        chart.element("slantLabels", "1");
        chart.element("anchoralpha", "100");
        chart.element("rotateLabels", "1");
        chart.element("showalternatevgridcolor", "0");
        chart.element("anchorbgalpha", "50");
        chart.element("showShadow", "0");
        chart.element("divLineIsDashed", "1");
        chart.element("showAlternateHGridColor", "0");
        chart.element("showAlternateVGridColor", "0");
        chart.element("alternateVGridColor", "ffffff");
        chart.element("adjustDiv", "1");
        chart.element("chartLeftMargin", "1");
        chart.element("valueOnRight", "1");
        chart.element("bgcolor", "FFFFFF");
        chart.element("canvasBgColor", "FFFFFF");
        chart.element("bordercolor", "FFFFFF");
        chart.element("canvasBorderThickness", "1");
        chart.element("showlegend", "1");
        chart.element("animation", "1");
        chart.element("divlinedecimalprecision", "1");
        chart.element("limitsdecimalprecision", "1");
        chart.element("shownames", "0");
        String resTypeId = String.valueOf(res.getResTypeId());
        if (resTypeId.equals("1201080100000") || resTypeId.equals("1201080200000") || resTypeId.equals("1201080300000")) {
            chart.element("showLabels ", "1");
            chart.element("chartLeftMargin", "50");
        } else {
            chart.element("showLabels ", "0");
            chart.element("lineThickness ", "1");
            chart.element("anchorRadius ", "2");
        }
        return chart;
    }

    private JSONObject buildJsonForProcessLine(String resId, List<Property> properties, List<PropertyValue> list, String title) {
        JSONObject json = new JSONObject();
        JSONObject chart = preparChart(resId);
        chart.element("numberSuffix", properties.get(0).getDataUnit() == null ? "" : properties.get(0).getDataUnit());
        JSONArray jj = new JSONArray();
        JSONObject o = new JSONObject();
        JSONArray labelJson = new JSONArray();
        for (PropertyValue p : list) {
            JSONObject obj = new JSONObject();
            obj.element("label", hourSdf.format(p.getTime()));
            labelJson.add(obj);
        }
        o.element("category", labelJson);
        jj.add(o);

        JSONArray dataset = new JSONArray();

        for (int j = 0; j < properties.size(); j++) {
            JSONObject obj = new JSONObject();
            obj.element("color", getRandomColor(j));
            JSONArray ja = new JSONArray();
            for (PropertyValue p : list) {
                JSONObject n = new JSONObject();
                String val = p.getValues()[j];
                n.element("value", (val == null || "".equals(val)) ? "0" : p.getValues()[j]);
                ja.add(n);
            }
            obj.element("data", ja);
            dataset.add(obj);
        }

        json.element("categories", jj);
        json.element("chart", chart);
        json.element("dataset", dataset);

        JSONObject result = new JSONObject();
        result.element("size", list.size());
        result.element("value", json);
        return result;

    }

    @Override
    public Object getModel() {
        return null;
    }

    @Override
    public String list() throws Exception {
        return null;
    }

    @Override
    public String input() throws Exception {
        return null;
    }

    @Override
    public String execute() throws Exception {
        String format = "yyyy-MM-dd ";
        SimpleDateFormat simple = new SimpleDateFormat(format);
        String endDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + " 23:59";
        String beginDate = simple.format(new Date(new Date().getTime() - 3600000 * 24 * 7)) + "00:00";
        Struts2Utils.setRequestAttribute("beginDate", beginDate);
        Struts2Utils.setRequestAttribute("endDate", endDate);
        Struts2Utils.setRequestAttribute("id", id);
        return SUCCESS;
    }

    @Override
    public String save() throws Exception {
        return null;
    }

    @Override
    public String delete() throws Exception {
        return null;
    }

    @Override
    protected void prepareModel() throws Exception {

    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

}
